package com.it.schoolbookshop_back.controller;

import com.it.schoolbookshop_back.entities.po.Category;
import com.it.schoolbookshop_back.entities.po.Result;
import com.it.schoolbookshop_back.service.CategoryService;
import com.it.schoolbookshop_back.utils.UserContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理控制器
 */
@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    
    /**
     * 获取所有分类列表
     * @return 包含书籍数量的分类列表
     */
    @GetMapping
    public Result<List<Category>> getAllCategories() {
        List<Category> categories = categoryService.getAllCategoriesWithBookCount();
        return Result.success("获取分类列表成功", categories);
    }
    
    /**
     * 获取指定分类详情，包含该分类下的书籍数量
     * @param id 分类ID
     * @return 包含书籍数量的分类详情
     */
    @GetMapping("/{id}")
    public Result<Category> getCategoryById(@PathVariable Integer id) {
        Category category = categoryService.getCategoryByIdWithBookCount(id);
        if (category == null) {
            return Result.failed("分类不存在");
        }
        return Result.success("获取分类详情成功", category);
    }
    

    

} 